/**
 * fileName:  FruitArgumentWriter.java
 * author:  huaimi
 * Company:  Cloudland Studio
 * Createdate:  2014-6-12 下午9:07:43
 *
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2011
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2014-6-12          huaimi            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.commercial.masterdata.store.write;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.cloudland.commercial.masterdata.store.DatabaseWrite;
import org.cloudland.commercial.masterdata.store.entity.FruitArgumentEntity;
import org.cloudland.jdbc.ibatis.masterdata.entity.ArgumentRelationEntity;
import org.cloudland.jdbc.ibatis.masterdata.handler.ArgumentRelationMapper;
import org.cloudland.util.Randoms;
import org.springframework.stereotype.Component;

 /**
 *  水果分类与参数关系领域层仓储写实现类，
 *  实现了领域层水果分类与参数关系的更新，新增及删除的持久化操作
 * @ClassName  FruitArgumentWriter
 * @author  huaimi
 * @version  %I%, %G%
 * @see  
 * @since   JDK1.6
 *
 */
@Component
public class FruitArgumentWriter  implements DatabaseWrite<FruitArgumentEntity>
{
	@Resource(name="argumentRelationMapper")
	private ArgumentRelationMapper argumentRelationMapper;
	
	/**
	 * 
	 * 批量保存水果与参数关系
	 *
	 * @param argumentsRelationList 水果对应参数集合
	 * @return 批量保存影响的行数
	 */
	public Integer batchSave(List<FruitArgumentEntity> argumentsRelationList)
	{
		
		List<ArgumentRelationEntity> entitys = new ArrayList<ArgumentRelationEntity>(argumentsRelationList.size());
		ArgumentRelationEntity argRel = null;
		StringBuffer costIds = null;
		for (FruitArgumentEntity argu : argumentsRelationList) {
			costIds = new StringBuffer();
			for (String costId : argu.getCostId()) {
				costIds.append(costId).append(",");
			}
			if (costIds.length() > 0) {
				costIds.deleteCharAt(costIds.length() - 1);
			}
			
			argRel = new ArgumentRelationEntity(Randoms.uuid(), argu.getFruitId(), argu.getArgumentId(), costIds.toString(), argu.getCostCustom());
			entitys.add(argRel);
		}
		
		return argumentRelationMapper.addBatch(entitys);
	}
	
	/**
	 * 
	 *  按水果编号删除水果对应的参数关系
	 *
	 * @param fruitId 水果编号
	 * @return 删除影响的行数
	 */
	public Integer deleteByFruitId(String fruitId)
	{
		argumentRelationMapper.deleteByFruitId(fruitId);
		return 1;
	}
	
	/**
	 * @param entity
	 */
	public void save(FruitArgumentEntity entity) {
	}

	/**
	 * @param entity
	 */
	public void update(FruitArgumentEntity entity) {
	}

	/**
	 * @param entity
	 */
	public void delete(FruitArgumentEntity entity) {
	}

}
